// pages/cart/index.js
import regeneratorRuntime from '../../lib/runtime/runtime';

Page({
  data: {
    address: {},
    cart: [],
    allChecked: false,
    totalPrice: 0,
    totalNum: 0
  },
  onShow() {
    const address = wx.getStorageSync("address");
    const cart = wx.getStorageSync("cart");
    this.setCart(cart);
    this.setData({
      address
    })
  },
  //点击收货地址
  handleChooseAddress() {
    wx.chooseAddress({
      success: (result) => {
        console.log(result);
        let address = result;
        address.all = address.provinceName + address.cityName + address.countyName + address.detailInfo;
        wx.setStorageSync("address", address);
      },

    });

  },

  // 商品的选中
  handleItemChange(e) {
    const goods_id = e.currentTarget.dataset.id;
    // 2 获取购物车数组
    let { cart } = this.data;
    // 3 找到被修改的商品对象
    let index = cart.findIndex(v => v.data.message.goods_id === goods_id);
    // 4 选中状态取反
    cart[index].checked = !cart[index].checked;
    // 把购物车数据重新设置回data和缓存中
    // this.setData({
    //   cart
    // });
    this.setCart(cart);

  },

  // 小封装
  // 设置购物车状态同时计算 底部工具栏的数据 全选 总价格 购买的数量
  setCart(cart) {
    // wx.setStorageSync("cart", cart);
    const allChecked = cart.length ? cart.every(v => v.checked) : false;
    let totalPrice = 0;
    let totalNum = 0;
    cart.forEach(v => {
      if (v.checked) {
        totalPrice += v.num * v.data.message.goods_price;
        totalNum += v.num;
      }
    });
    this.setData({
      cart,
      allChecked,
      totalPrice,
      totalNum
    });
    wx.setStorageSync('cart', cart);

  },

  // 商品全选功能
  handleItemAllCheck() {
    let { cart, allChecked } = this.data;

    allChecked = !allChecked;

    cart.forEach(v => v.checked = allChecked)
    // 把修改后的值 填充回data或缓存中
    // this.setData({ cart, allChecked })
    this.setCart(cart)
  },

  // 商品数量的编辑功能
  handleItemNumEdit(e) {
    // 获取传递过来的参数
    const { operation, id } = e.currentTarget.dataset;
    console.log(operation);
    // 获取购物车数组
    let { cart } = this.data;
    // 获取需要修改的索引
    const index = cart.findIndex(v => v.data.message.goods_id === id);
    // 商品数量为 1 还减 判断是否删除商品
    if (cart[index].num === 1 && operation === -1) {
      wx.showModal({
        title: '提示',
        content: '是否删除？',
        showCancel: true,
        cancelText: '取消',
        cancelColor: '#000000',
        confirmText: '确定',
        confirmColor: '#3CC51F',
        success: (result) => {
          if (result.confirm) {
            cart.splice(index, 1);
            this.setCart(cart)
          }
        },
      });
    } else {
      cart[index].num += operation;
      this.setCart(cart)
    }

    // cart[index].num += operation;

    // this.setCart(cart)
  },

  // 结算
  handlePay() {
    const { address, totalNum } = this.data;
    if (!address.userName) {
      wx.showModal({
        title: '提示',
        content: '未填地址',
        showCancel: false,
        cancelColor: '#000000',
        confirmText: '确定',
        confirmColor: '#3CC51F',
      });
      return
    }

    if (totalNum === 0) {
      wx.showModal({
        title: '提示',
        content: '未选商品',
        showCancel: false,
        cancelColor: '#000000',
        confirmText: '确定',
        confirmColor: '#3CC51F',
      });
      return
    }

    wx.navigateTo({
      url: '/pages/pay/index',
    });

  }
})
